package com.xxl.javaWork_15;

import javax.swing.*;
import java.awt.*;

/**
 * @Author: xiaxinlin
 * @Date: 2020/12/14 16:05
 */
public class SinRunBall extends JFrame implements Runnable {

    private int x=0;
    private int y =0;
    private double speed = Math.PI;
    private double time=0;
    private int width=500;
    private int height=500;

    public static void main(String[] args) {
        new Thread(new SinRunBall()).start();
    }

    public SinRunBall(){
        setSize(width,height);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    @Override
    public void run() {
        while (true){
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            x= (int) (x+speed);
            y= (int) (100*Math.sin(2*time));
            time+=0.05;
            if (x>width){
                x=0;
            }
            repaint();
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.setColor(Color.green);
        g.fillOval(x,height/2-y,40,40);
    }
}
